package com.chinacoal.hr.levelimpl.service.impl;

import com.chinacoal.hr.levelapi.entity.TransPaperDetail;
import com.chinacoal.hr.levelimpl.dao.TransPaperDetailDao;
import com.chinacoal.hr.levelimpl.service.TransPaperDetailService;
import com.chinacoal.hr.levelimpl.utils.StaticDicts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2019-11-18 15:39
 * @description:
 **/
@Service
public class TransPaperDetailServiceImpl implements TransPaperDetailService {

    @Autowired
    private TransPaperDetailDao transPaperDetailDao;


    @Override
    public Map<String, String> insertTransPaperDetail(TransPaperDetail record) throws Throwable {
        Map<String, String> result = new HashMap<String, String>();
        result.put("code", "-1");

        try {
            transPaperDetailDao.insertTransPaperDetail(record);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

        result.put("code", "1");
        return result;
    }

    @Override
    public Map<String, String> updateTransPaperDetail(TransPaperDetail record) throws Throwable {
        Map<String, String> result = new HashMap<String, String>();
        result.put("code", "-1");

        try {
            transPaperDetailDao.updateTransPaperDetail(record);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

        result.put("code", "1");
        return result;
    }


    /**
     * 提交请假申请单时配套调用此方法
     *
     * @param paDocId     : 单据ID
     * @param empCode     : 员工code
     * @param createDate  : 创建时间，根据创建时间区分年月
     * @param holidayType : 假期类型
     * @param leaveDays   : 请假天数
     * @return : void
     * @author : YunTao.Li
     * @date : 2019/11/19 2019/11/19
     */
    public void submitTransPaperDetail(String paDocId,
                                       String empCode,
                                       Date createDate,
                                       String holidayType,
                                       String leaveDays) throws Throwable {

        // step1 : 根据创建时间获得年、月
        LocalDate currentLocalDate = LocalDate.now();
        int year = currentLocalDate.getYear();
        int month = currentLocalDate.getMonthValue();

        TransPaperDetail paperDetail = new TransPaperDetail();
        paperDetail.setPaDocId(paDocId);
        paperDetail.setDocTypeId(StaticDicts.DOC_TYPE_ID_LEAVE);// 如果只有请假申请单有这个表的数据，则这个字段固定为请假申请单的类型
//        paperDetail.setEmpId(empCode);
        paperDetail.setEmpCode(empCode);
        paperDetail.setSumType(StaticDicts.SUM_TYPE_APPLYING); //申请中
        paperDetail.setYear(String.valueOf(year)); //按请假单的创建时间来确定年月
        paperDetail.setMonth(String.valueOf(month));
        paperDetail.setHolidayType(holidayType);
        paperDetail.setLeaveDays(leaveDays);

        this.insertTransPaperDetail(paperDetail);
    }
}
